import { pageToLogin } from "./utils"
import { HTTP_STATUS } from './config'


const interceptors = (res: UniApp.RequestSuccessCallbackResult) => {
  // 只要请求成功，不管返回什么状态码，都走这个回调
  if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
    return Promise.reject("请求资源不存在")

  } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
    return Promise.reject("服务端出现了问题")

  } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
    uni.setStorageSync("token", "")
    pageToLogin()
    // TODO 根据自身业务修改
    return Promise.reject("没有权限访问");

  } else if (res.statusCode === HTTP_STATUS.AUTHENTICATE) {
    uni.setStorageSync("token", "")
    pageToLogin()
    return Promise.reject("需要鉴权")

  } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
    return Promise.resolve(res.data)

  }
  return Promise.reject("其他错误");
}

export default interceptors
